package main

import (
	"fmt"

	"gitee.com/knowgo/rjhttp/pkg/rjhc"
)

type IconsInGroupRequest struct {
	Group string `json:"group" rj:"desc:分组"`
}

type SvgAttr struct {
	Id            string `json:"id,omitempty"`
	Stroke        string `json:"stroke,omitempty"`
	StrokeWidth   string `json:"strokeWidth,omitempty"`
	Fill          string `json:"fill,omitempty"`
	FillRule      string `json:"fillRule,omitempty" rj:"desc:此配置项不可用"`
	Transform     string `json:"transform,omitempty"`
	StrokeOpacity string `json:"strokeOpacity,omitempty"`
	FillOpacity   string `json:"fillOpacity,omitempty"`
	Dx            string `json:"dx,omitempty"`
	Dy            string `json:"dy,omitempty"`
	D             string `json:"d,omitempty"`  // path
	Cx            string `json:"cx,omitempty"` // circle
	Cy            string `json:"cy,omitempty"`
	R             string `json:"r,omitempty"`
	Style         string `json:"style,omitempty"`
	X             string `json:"x,omitempty"` // rect
	Y             string `json:"y,omitempty"`
	Width         string `json:"width,omitempty"`
	Height        string `json:"height,omitempty"`
	Rx            string `json:"rx,omitempty"` // rect 圆角 ellipse rx 属性定义水平半径
	Ry            string `json:"ry,omitempty"` // rect 圆角 ellipse ry 属性定义垂直半径
	Opacity       string `json:"opacity,omitempty"`
	X1            string `json:"x1,omitempty"` // line
	Y1            string `json:"y1,omitempty"`
	X2            string `json:"x2,omitempty"`
	Y2            string `json:"y2,omitempty"`
	Points        string `json:"points,omitempty"` // polyline polygon
}

func (el *SvgAttr) ToString() string {
	str := ""
	if el.Stroke != "" {
		str += fmt.Sprintf(" stroke='%s'", el.Stroke)
	}
	if el.StrokeWidth != "" {
		str += fmt.Sprintf(" strokeWidth='%s'", el.StrokeWidth)
	}
	if el.Fill != "" {
		str += fmt.Sprintf(" fill={{ color || '%s'}}", el.Fill)
	} else {
		str += " fill={{ color || 'currentColor'}}"
	}
	if el.Transform != "" {
		str += fmt.Sprintf(" transform='%s'", el.Transform)
	}
	if el.StrokeOpacity != "" {
		str += fmt.Sprintf(" strokeOpacity='%s'", el.StrokeOpacity)
	}
	if el.FillOpacity != "" {
		str += fmt.Sprintf(" fillOpacity='%s'", el.FillOpacity)
	}
	if el.Dx != "" {
		str += fmt.Sprintf(" dx='%s'", el.Dx)
	}
	if el.Dy != "" {
		str += fmt.Sprintf(" dy='%s'", el.Dy)
	}
	if el.D != "" {
		str += fmt.Sprintf(" d='%s'", el.D)
	}
	if el.Cx != "" {
		str += fmt.Sprintf(" cx='%s'", el.Cx)
	}
	if el.Cy != "" {
		str += fmt.Sprintf(" cy='%s'", el.Cy)
	}
	if el.R != "" {
		str += fmt.Sprintf(" r='%s'", el.R)
	}
	if el.Style != "" {
		str += fmt.Sprintf(" style='%s'", el.Style)
	}
	if el.X != "" {
		str += fmt.Sprintf(" x='%s'", el.X)
	}
	if el.Y != "" {
		str += fmt.Sprintf(" y='%s'", el.Y)
	}
	if el.Width != "" {
		str += fmt.Sprintf(" width='%s'", el.Width)
	}
	if el.Height != "" {
		str += fmt.Sprintf(" height='%s'", el.Height)
	}
	if el.Rx != "" {
		str += fmt.Sprintf(" rx='%s'", el.Rx)
	}
	if el.Ry != "" {
		str += fmt.Sprintf(" ry='%s'", el.Ry)
	}
	if el.Opacity != "" {
		str += fmt.Sprintf(" opacity='%s'", el.Opacity)
	}
	if el.X1 != "" {
		str += fmt.Sprintf(" x1='%s'", el.X1)
	}
	if el.Y1 != "" {
		str += fmt.Sprintf(" y1='%s'", el.Y1)
	}
	if el.X2 != "" {
		str += fmt.Sprintf(" x1='%s'", el.X2)
	}
	if el.Y2 != "" {
		str += fmt.Sprintf(" y2='%s'", el.Y2)
	}
	if el.Points != "" {
		str += fmt.Sprintf(" points='%s'", el.Points)
	}
	return str
}

type SvgTag struct {
	Tag      string    `json:"name" rj:"desc:tag 标签名称"`
	Attrs    *SvgAttr  `json:"attrs" rj:"desc:属性列表"`
	Elements []*SvgTag `json:"elements,omitempty" rj:"desc:下属属性列表"`
}

// <view wx:if="{{name === 'success'}}" style="background-image: url({{quot}}data:image/svg+xml, %3Csvg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg' width='{{svgSize}}px' height='{{svgSize}}px'%3E%3Cpath d='M512 1024a512 512 0 1 1 0-1024 512 512 0 0 1 0 1024z m-71.318588-361.411765a29.334588 29.334588 0 0 0 20.48-8.252235L774.625882 349.364706a27.708235 27.708235 0 0 0 0-39.936 29.575529 29.575529 0 0 0-41.08047 0l-292.74353 284.912941L290.454588 448.150588a29.575529 29.575529 0 0 0-41.08047 0 27.708235 27.708235 0 0 0 0 39.996236l170.706823 166.128941a29.274353 29.274353 0 0 0 20.540235 8.252235z' fill='{{(isStr ? colors : colors[0]) || 'rgb(51,169,84)'}}' /%3E%3C/svg%3E{{quot}}); width: {{svgSize}}px; height: {{svgSize}}px; " class="icon" />
func (s *SvgTag) ToString() string {
	elements := ""
	for _, el := range s.Elements {
		elements += el.ToString()
	}
	if elements == "" {
		return fmt.Sprintf("%%3C%s%s /%%3E", s.Tag, s.Attrs.ToString())
	}
	return fmt.Sprintf("%%3C%s%s %%3E%s%%3C/%s%%3E", s.Tag, s.Attrs.ToString(), elements, s.Tag)
}

type IconInfo struct {
	Name     string    `json:"name" rj:"desc:图标名称"`
	Elements []*SvgTag `json:"elements,omitempty" rj:"desc:SVG 对象列表"`
	ViewBox  string    `json:"viewBox" rj:"desc:裁剪框"`
	Groups   []string  `json:"groups" rj:"desc:分组"`
}

// <view wx:if="{{name === 'success'}}" style="background-image: url({{quot}}data:image/svg+xml, %3Csvg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg' width='{{svgSize}}px' height='{{svgSize}}px'%3E%3Cpath d='M512 1024a512 512 0 1 1 0-1024 512 512 0 0 1 0 1024z m-71.318588-361.411765a29.334588 29.334588 0 0 0 20.48-8.252235L774.625882 349.364706a27.708235 27.708235 0 0 0 0-39.936 29.575529 29.575529 0 0 0-41.08047 0l-292.74353 284.912941L290.454588 448.150588a29.575529 29.575529 0 0 0-41.08047 0 27.708235 27.708235 0 0 0 0 39.996236l170.706823 166.128941a29.274353 29.274353 0 0 0 20.540235 8.252235z' fill='{{(isStr ? colors : colors[0]) || 'rgb(51,169,84)'}}' /%3E%3C/svg%3E{{quot}}); width: {{svgSize}}px; height: {{svgSize}}px; " class="icon" />
func (i *IconInfo) ToWxIcon() string {
	svg := "%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='" + i.ViewBox + " width='{{iconSize}}px' height='{{iconSize}}px'"
	elments := ""
	for _, el := range i.Elements {
		elments += el.ToString()
	}
	svg += elments
	svg += "%3C/svg%3E"
	str := "<view wx:if=\"{{ name === '" + i.Name + "' }}\" style=\"width: {{iconSize}}px height: {{iconSize}}px; background-image: url({{quot}}data:image/svg+xml, " + svg + ")\" />"
	return str
}

func main() {
	clt := rjhc.NewClient("127.0.0.1", "api", 6789)
	clt.LogRequest()
	err := clt.PostOne(&rjhc.InvokeObject{
		Service: "icon.GetGroupIcons",
		Args:    &IconsInGroupRequest{Group: "mp-medical"},
	}, func(res *rjhc.Response) error {
		ary := res.Data.Response["icon.GetGroupIcons"]
		ma := res.Data.Response
		for k, v := range ma {
			fmt.Println(k, v)
		}
		fmt.Println(ary)
		return nil
	})
	if err != nil {
		panic(err)
	}
}
